//package com.bosssoft.g1.common.core.filter;
//
//import cn.hutool.core.util.StrUtil;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Component;
//import javax.servlet.*;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.Collections;
//import java.util.Map;
//
//@Component
//public class TokenFilter implements Filter {
//
//    private static final Logger logger = LoggerFactory.getLogger(TokenFilter.class);
//
//    private static final String AUTH_HEADER = "Authorization";
//    private static final String BEARER_SCHEME = "Bearer ";
//    private static final String PASS_TOKEN = "yuxingzhtest";
//    private static final Map<String, Boolean> VALID_TOKENS = Collections.singletonMap("your_valid_token_here", true);
//    // 定义登录路径
//    private static final String LOGIN_PATH = "/login";
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        // Filter initialization can be done here if needed
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
//            throws IOException, ServletException {
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        String authHeader = request.getHeader(AUTH_HEADER);
//        String requestPath = request.getRequestURI();
//        logger.info("requestPath" + requestPath);
//        // 检查请求路径是否为登录路径
//        if (requestPath.contains(LOGIN_PATH)) {
//            // 如果是登录路径，直接放行
//            filterChain.doFilter(servletRequest, servletResponse);
//            return;
//        }
//
//        if (authHeader != null && authHeader.startsWith(BEARER_SCHEME)) {
//            String token = authHeader.substring(BEARER_SCHEME.length());
//            System.out.println(token);
//            boolean isValidToken = VALID_TOKENS.containsKey(token) && VALID_TOKENS.get(token) || StrUtil.equals(PASS_TOKEN, token);
//            if (isValidToken) {
//                // Token is valid, continue the filter chain
//                filterChain.doFilter(servletRequest, servletResponse);
//            } else {
//                // Token is not valid, send 401 Unauthorized response
//                HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
//                httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//                httpServletResponse.setHeader("WWW-Authenticate", "Bearer error=\"invalid_token\"");
//                logger.warn("Invalid token presented.");
//            }
//        } else {
//            // No token presented, send 401 Unauthorized response
//            HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
//            httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//            httpServletResponse.setHeader("WWW-Authenticate", "Bearer error=\"missing_token\"");
//            logger.warn("No token presented.");
//        }
//    }
//
//    @Override
//    public void destroy() {
//        // Clean up resources if needed
//    }
//}